﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Nemesis;
using Nemesis.WoW.ObjectManager;

namespace Nemesis.CustomClasses
{
    class DefaultWarriorArms : Nemesis.CustomClasses.CCBase
    {
        private WowLocalPlayer Me = ObjectManager.Me;
        private bool AutoMovement = true;

        public override string Name
        {
            get { return "DefaultWarriorArms"; }
        }

        public override void Pull(Nemesis.WoW.ObjectManager.WowUnit Unit)
        {
            CastSpell("Battle Shout", !Me.HasAura("Battle Shout"), Me);
            CastSpell("Battle Stance", !Me.HasAura("Battle Stance"), Me);
            CastSpell("Charge", true, Unit);                       
        }


        public override void CombatLoop(Nemesis.WoW.ObjectManager.WowUnit Unit)
        {
            WoW.Classes.Timeout timeout = new WoW.Classes.Timeout(3000);
            while (WoW.Tools.Spells.GlobalCooldown && !timeout.IsTimedOut)
                Thread.Sleep(10);
                    
            if (CastSpell("Die by the Sword", Me.GetNumAttackers > 1 || Me.HealthPercentage < 20, Unit)) return;
            if (CastSpell("Recklessness", Me.GetNumAttackers > 1, Unit)) return;

            if (CastSpell("Execute", true, Unit)) return;
            if (CastSpell("Pummel", Unit.CanInterruptSpell, Unit)) return;
            if (CastSpell("Hamstring", Unit.IsFleeing, Unit)) return;         
            if (CastSpell("Victory Rush", Me.HealthPercentage < 75, Unit)) return; // auto cast "Impending Victory" as Override
            if (CastSpell("Colossus Smash", !Unit.HasAura("Colossus Smash"), Unit)) return;
            if (CastSpell("Rend", !Unit.HasAura("Rend") && !Unit.HasAura("Enhanced Rend"), Unit)) return;
            if (CastSpell("Heroic Strike", true, Unit)) return;  // auto cast "Mortal Strike" as Override
            if (CastSpell("Dragon Roar", true, Unit)) return;
            if (CastSpell("Whirlwind", true, Unit)) return;
            if (CastSpell("Auto Attack", !Me.IsAutoAttacking, Unit)) return;
            
        }

        public override void PostCombat()
        {
        }

        public override void Patrolling()
        {
        }


        public bool CastSpell(string spellname, bool condition, WoW.ObjectManager.WowUnit targetUnit)
        {
            if (!condition)
                return false;
            
            WoW.Classes.WowSpell wowSpell = WoW.Tools.Spells.GetWoWSpellOrOverrideByName(spellname);
            
            if (!wowSpell.IsValid || !wowSpell.IsUsable)
                return false;

            if (AutoMovement)
            {
                int minRange = (int)wowSpell.MinRange;
                int maxRange = (int)wowSpell.MaxRange;

                // Maybe move back here
                if (minRange > 0 && targetUnit.DistanceToMe < minRange)
                    return false;

                // Move forward in Range
                if (maxRange > 0 && targetUnit.DistanceToMe > maxRange)
                    WoW.Actions.Approach.Unit(targetUnit, maxRange - 1, true);
            }

            if (targetUnit.IsLocalPlayer)
                WoW.Actions.SpellManager.CastSpellByID(wowSpell.SpellId, true, false);
            else
            {
                if (Me.Target.BaseAddress != targetUnit.BaseAddress)
                    targetUnit.SetTarget();

                WoW.Actions.SpellManager.CastSpellByID(wowSpell.SpellId, targetUnit, false);
            }
            return false;
        }

        #region unused
        public override void Custom1() { }
        public override void Custom2() { }
        public override void Custom3() { }
        public override void Custom4() { }
        public override void Custom5() { }
        public override void Custom6() { }
        public override void Custom7() { }
        public override void Custom8() { }
        public override void Custom9() { }
        public override void Custom10() { }
        #endregion
    }
}
